package com.cy.pj.sys.web.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;

/**
 * @DATE: 2021/3/12 9:08
 * @Author: LuckyH
 */

// 拦截器 spring web模块 实现HandlerInterceptor
public class TimeAccessInterceptor implements HandlerInterceptor {

    // 后端执行之前执行 请求 响应对象，指向处理器对象 是否放行

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        System.out.println("---------interceptor---------");
        // 返回系统当前时间
        LocalTime localTime = LocalTime.now();
        int hour = localTime.getHour();
        System.out.println(hour);
        if (hour<9 || hour>22)
            throw new RuntimeException("请在指定时间点访问！");
        return true;
    }
}
